
<template>
    <!-- eslint-disable vue/require-component-is -->
    <component v-bind="linkProps(to)">
        <slot />
    </component>
</template>

<script>
/* eslint-disable no-new */
export default {
    props: {
        to: {
            type: String,
            required: true
        }
    },
    methods: {
        /**
         * @param {string} path
         * @returns {Boolean}
         */
        isExternal(path) {
            return /^(http?:|https?:|mailto:|tel:)/.test(path)
        },
        linkProps(url) {
            if (this.isExternal(url)) {
                return {
                    is: 'a',
                    href: url,
                    target: '_blank',
                    rel: 'noopener'
                }
            }
            return {
                is: 'router-link',
                to: url
            }
        }
    }
}
</script>
